home *** CD-ROM | disk | FTP | other *** search
- /* lseek.c --- p 497 */
- #include <stdio.h>
- #include <io.h>
- #include <fcntl.h>
- main()
- {
- int fhandle, count,
- long curpos;
- unsigned char buffer[20];
- /* Open the file "autoexec.bat." */
- if ( (fhandle = open("c:\\autoexec.bat", O_RDONLY)) == -1)
- {
- printf("open failed");
- exit(1);
- }
- /* Go to end of file using "lseek" and SEEK_END */
- curpos = lseek(fhandle, OL, SEEK_END);
- printf("End of file in 'autoexec.bat' is %ld bytes from beginning\n",
- curpos);
- /* Move back 20 bytes, read the last 20 bytes
- * and print the characters that were read in. */
- lseek(fhandle, -20L, SEEK_CUR);
- if ((count = read(fhandle, buffer, 20)) == -1)
- {
- perror("read error");
- exit(1);
- }
- printf("The last 20 characters in 'autoexec.bat' are:\n");
- write(1, buffer, count);
- }